package com.wskh.edu.controller;


import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.api.naming.pojo.Instance;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wskh.commonUtil.R;
import com.wskh.edu.client.OssClient;
import com.wskh.edu.client.VodClient;
import com.wskh.edu.entity.Chapter;
import com.wskh.edu.entity.Video;
import com.wskh.edu.entity.chapter.ChapterVo;
import com.wskh.edu.service.ChapterService;
import com.wskh.edu.service.VideoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author wskh
 * @since 2021-10-14
 */
@Api(value="课程章节管理")
@RestController
@RequestMapping("/eduService/chapter")
@CrossOrigin(origins = "*", methods = {RequestMethod.GET, RequestMethod.POST, RequestMethod.DELETE}) // 允许请求跨域
public class ChapterController {
    @Autowired
    private ChapterService chapterService;

    @Autowired
    VideoService videoService;

    @Autowired
    VodClient vodClient;

    @Autowired
    OssClient ossClient;

    @ApiOperation(value = "根据课程id获取章节信息")
    @GetMapping("getChapterVideoByCourseId/{courseId}")
    public R getChapterVideoByCourseId(@ApiParam(name = "courseId", value = "课程ID", required = true)
                                  @PathVariable String courseId){
        List<ChapterVo> chapterVoList = chapterService.getChapterVideoByCourseId(courseId);
        return R.ok().data("items", chapterVoList);
    }

    @ApiOperation(value = "新增章节")
    @PostMapping("addChapter")
    public R addChapter(
            @ApiParam(name = "chapterVo", value = "章节对象", required = true)
            @RequestBody Chapter chapter){
        chapterService.save(chapter);
        return R.ok();
    }

    @ApiOperation(value = "根据ID查询章节")
    @GetMapping("getChapterById/{id}")
    public R getChapterById(
            @ApiParam(name = "id", value = "章节ID", required = true)
            @PathVariable String id){
        Chapter chapter = chapterService.getById(id);
        return R.ok().data("item", chapter);
    }

    @ApiOperation(value = "根据ID修改章节")
    @PostMapping("updateChapterById/{id}")
    public R updateChapterById(
            @ApiParam(name = "id", value = "章节ID", required = true)
            @PathVariable String id,
            @ApiParam(name = "chapter", value = "章节对象", required = true)
            @RequestBody Chapter chapter){
        chapter.setId(id);
        chapterService.updateById(chapter);
        return R.ok();
    }

    @ApiOperation(value = "根据ID删除章节")
    @DeleteMapping("removeChapterById/{id}")
    public R removeChapterById(
            @ApiParam(name = "id", value = "章节ID", required = true)
            @PathVariable String id){
        boolean result = chapterService.removeChapterById(id);
        if(result){
            return R.ok();
        }else{
            return R.error().message("章节删除失败");
        }
    }

    @PostMapping("test/{id}")
    public R test(@PathVariable String id) throws NacosException {
        System.out.println("id = "+id);
        return ossClient.deleteVideoById(id);
    }

}

